package com.zr.service.impl;

import com.github.pagehelper.PageHelper;
import com.zr.dao.ArticleCateDao;
import com.zr.dao.ArticleDao;
import com.zr.dao.UserDao;
import com.zr.pojo.Article;
import com.zr.pojo.ArticleCate;
import com.zr.pojo.vo.ArticleVO;
import com.zr.service.ArticleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class ArticleServiceImpl implements ArticleService {

    private ArticleDao articleDao;
    private UserDao userDao;
    private ArticleCateDao articleCateDao;

    @Override
    public Boolean add(ArticleVO articleVO, String fileName, String userName) {
//        TODO 先查询是否存在未删除的同名title文章 略
        Article article = new Article();
        article.setAuthor_id(userDao.selectByUsername(userName).getId());
        article.setCover_img(fileName);
        article.setCate_id(articleVO.getCate_id());
        article.setContent(articleVO.getContent());
        article.setPub_date(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        article.setState(articleVO.getState());
        article.setTitle(articleVO.getTitle());
        article.setIs_delete(0);
        return articleDao.add(article)==1;
    }

    @Override
    public Boolean deleteById(int id) {
        return articleDao.delete(id)==1;
    }


    @Override
    public List<Article> selectAll(int pagenum, int pagesize, int cate_id, String state) {
        PageHelper.startPage(pagenum, pagesize);
        List<Article> articles = articleDao.selectWithConditions(cate_id, state);
        //前端要求返回的数据属性值
        for (Article article : articles) {
            int cate_id1 = article.getCate_id();
            ArticleCate articleCate = articleCateDao.selectById(cate_id1);
            String name = articleCate.getName();
            article.setCate_name(name);
        }
        return articles;
    }

    @Override
    public Article selectById(int id) {
        Article article = articleDao.selectByID(id);
        //前端要求返回的数据属性值
        Integer cate_id = article.getCate_id();
        ArticleCate articleCate = articleCateDao.selectById(cate_id);
        String name = articleCate.getName();
        article.setCate_name(name);
        return article;
    }


    @Override
    public Boolean updateById(ArticleVO articleVO, String fileName) {
        Article article = new Article();
        article.setId(articleVO.getId());
        //作者一经确定不可更改
//        article.setAuthor_id(userDao.selectByUsername(userName).getId());
        article.setCover_img(fileName);
        article.setCate_id(articleVO.getCate_id());
        article.setContent(articleVO.getContent());
        article.setPub_date(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        article.setState(articleVO.getState());
        article.setTitle(articleVO.getTitle());
        article.setIs_delete(0);
        return articleDao.updateById(article)==1;
    }
}
